Imports System.Drawing

''' <summary>
''' Describes the interior layout of a chart.
''' </summary>
''' <remarks></remarks>
''' 
Public Class ChartDimensions
    Protected miTitleHeight As Integer = 0

    Protected miFrameWidthLeft As Integer = 10
    Protected miFrameWidthRight As Integer = 2
    Protected miFrameWidthTop As Integer = 10
    Protected miFrameWidthBottom As Integer = 20

    Protected moFrameColor As System.Drawing.Color = Color.White

    '---Colors
    Protected moBackgroundColor As System.Drawing.Color = Color.Beige
    Protected moXAxisBackgroundColor As Color = Color.White
    Protected moYAxis1BackgroundColor As Color = Color.White
    Protected moYAxis2BackgroundColor As Color = Color.White

    '---Dimensions of entire chart
    Protected miOuterX As Integer
    Protected miOuterY As Integer
    Protected miOuterWidth As Integer
    Protected miOuterHeight As Integer

    '---Axis areas
    Protected miXAxisLabelRowHeight As Integer = 20
    Protected miYAxis1LabelColumnWidth As Integer = 20
    Protected miYAxis2LabelColumnWidth As Integer = 0

    '---Axis label dimensions
    Protected miXAxisLabelHeight As Integer = 0
    Protected miYAxis1LabelWidth As Integer = 0
    Protected miYAxis2LabelWidth As Integer = 0

    '---Dimensions of drawing area of chart (excludes x & y axis labels)
    Protected miInnerLeft As Integer
    Protected miInnerTop As Integer

    Protected miInnerRight As Integer
    Protected miInnerBottom As Integer

    Protected miInnerWidth As Integer
    Protected miInnerHeight As Integer

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="iWidth">The width of the generated chart image.</param>
    ''' <param name="iHeight">The height of the generated chart image.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal iWidth As Integer, ByVal iHeight As Integer)
        '---Outer chart dimensions
        miOuterWidth = iWidth
        miOuterHeight = iHeight

        Calibrate()
    End Sub

    Private Sub Calibrate()
        '---Inner chart dimensions
        miInnerTop = miOuterY + miFrameWidthTop
        miInnerLeft = miOuterX + miFrameWidthLeft + miYAxis1LabelColumnWidth

        miInnerWidth = miOuterX + miOuterWidth - ((miFrameWidthLeft + miFrameWidthRight) + miYAxis1LabelColumnWidth + miYAxis2LabelColumnWidth)
        miInnerHeight = (miOuterHeight - miTitleHeight) - ((miFrameWidthTop + miFrameWidthBottom) + miXAxisLabelRowHeight)

        miInnerBottom = miInnerTop + miInnerHeight
        miInnerRight = miInnerLeft + miInnerWidth
    End Sub

    '---Chart dimensions
    ''' <summary>
    ''' The left hand position of the charting area within the final image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OuterX() As Integer
        Get
            Return miOuterX
        End Get
        Set(ByVal value As Integer)
            miOuterX = value
        End Set
    End Property

    ''' <summary>
    ''' The topmost position of the charting area within the final image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OuterY() As Integer
        Get
            Return miOuterY
        End Get
        Set(ByVal value As Integer)
            miOuterY = value
        End Set
    End Property

    ''' <summary>
    ''' Width of the chart image (same as iWidth from the constructor)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OuterWidth() As Integer
        Get
            Return miOuterWidth
        End Get
        Set(ByVal value As Integer)
            miOuterWidth = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Height of the chart image (same as iHeight from the constructor)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OuterHeight() As Integer
        Get
            Return miOuterHeight - miTitleHeight
        End Get
        Set(ByVal value As Integer)
            miOuterHeight = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Left position of the chart area within the chart image. 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Note: Changing this value will cause the InnerRight value to change as well.</remarks>
    Public Property InnerLeft() As Integer
        Get
            Return miInnerLeft
        End Get
        Set(ByVal value As Integer)
            miInnerLeft = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Top position of the chart area within the chart image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Changing this value will cause the InnerRight value to change as well.</remarks>
    Public Property InnerTop() As Integer
        Get
            Return miInnerTop
        End Get
        Set(ByVal value As Integer)
            miInnerTop = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Right position of the chart area within the chart image.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Changing this value will cause the InnerLeft value to change as well.</remarks>
    Public Property InnerRight() As Integer
        Get
            Return miInnerRight
        End Get
        Set(ByVal value As Integer)
            miInnerRight = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Bottom position of the chart area within the chart image. 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Changing this value will cause the InnerTop value to change as well.</remarks>
    Public Property InnerBottom() As Integer
        Get
            Return miInnerBottom
        End Get
        Set(ByVal value As Integer)
            miInnerBottom = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Width of chart area within chart image.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Changing this value will cause the InnerRight value to change as well.</remarks>
    Public Property InnerWidth() As Integer
        Get
            Return miInnerWidth
        End Get
        Set(ByVal value As Integer)
            miInnerWidth = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Height of the chart area within the chart image. 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Changing this value will cause the InnerBottom value to change as well.</remarks>
    Public Property InnerHeight() As Integer
        Get
            Return miInnerHeight
        End Get
        Set(ByVal value As Integer)
            miInnerHeight = value
            Calibrate()
        End Set
    End Property

    '---Height of XAxis
    ''' <summary>
    ''' Height of the area at the bottom of the chart reserved for X Axis labels.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 20</remarks>
    Public Property XAxisLabelRowHeight() As Integer
        Get
            Return miXAxisLabelRowHeight
        End Get
        Set(ByVal value As Integer)
            miXAxisLabelRowHeight = value
            Calibrate()
        End Set
    End Property

    '---Width of YAxis1
    ''' <summary>
    ''' Width of the area at the left of the chart reserved for Y Axis 1 labels.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 20</remarks>
    Public Property YAxis1LabelColumnWidth() As Integer
        Get
            Return miYAxis1LabelColumnWidth
        End Get
        Set(ByVal value As Integer)
            miYAxis1LabelColumnWidth = value
            Calibrate()
        End Set
    End Property

    '---Width of YAxis2
    ''' <summary>
    ''' Width of the area at the right of the chart reserved for Y Axis 2 labels.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 0</remarks>
    Public Property YAxis2LabelColumnWidth() As Integer
        Get
            Return miYAxis2LabelColumnWidth
        End Get
        Set(ByVal value As Integer)
            miYAxis2LabelColumnWidth = value
            Calibrate()
        End Set
    End Property

    '---Width of border around outside of chart
    ''' <summary>
    ''' The Frame is the margin area between the chart area and the edge of the chart image. 
    ''' The FrameSize property sets the FrameWidthTop, FrameWidthLeft, FrameWidthBottom and 
    ''' FrameWidthRight values to the same value for uniform framewidth.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public WriteOnly Property FrameSize() As Integer
        Set(ByVal value As Integer)
            FrameWidthTop = value
            FrameWidthBottom = value
            FrameWidthLeft = value
            FrameWidthRight = value
        End Set
    End Property

    ''' <summary>
    ''' The amount of space reserved at the top of the chart for the chart title.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property TitleHeight() As Integer
        Get
            Return miTitleHeight
        End Get
        Set(ByVal value As Integer)
            miTitleHeight = value
            miOuterY = value

            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Sets or gets the height of the frame along the top of the chart area.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 10</remarks>
    Public Property FrameWidthTop() As Integer
        Get
            Return miFrameWidthTop
        End Get
        Set(ByVal value As Integer)
            miFrameWidthTop = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Sets or gets the height of the frame along the bottom of the chart area.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 20</remarks>
    Public Property FrameWidthBottom() As Integer
        Get
            Return miFrameWidthBottom
        End Get
        Set(ByVal value As Integer)
            miFrameWidthBottom = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Sets or gets the width of the frame along the left side of the chart area.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 10</remarks>
    Public Property FrameWidthLeft() As Integer
        Get
            Return miFrameWidthLeft
        End Get
        Set(ByVal value As Integer)
            miFrameWidthLeft = value
            Calibrate()
        End Set
    End Property

    ''' <summary>
    ''' Sets or gets the width of the frame along the right side of the chart area.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to 2</remarks>
    Public Property FrameWidthRight() As Integer
        Get
            Return miFrameWidthRight
        End Get
        Set(ByVal value As Integer)
            miFrameWidthRight = value
            Calibrate()
        End Set
    End Property

    '---Background Color
    ''' <summary>
    ''' Sets or gets the background color of the chart image.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to Color.Beige</remarks>
    Public Property BackgroundColor() As Color
        Get
            Return moBackgroundColor
        End Get
        Set(ByVal value As Color)
            moBackgroundColor = value
        End Set
    End Property

    '---Color of border around outside of chart
    ''' <summary>
    ''' Sets or gets the color of the frame area.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to Color.White</remarks>
    Public Property FrameColor() As Color
        Get
            Return moFrameColor
        End Get
        Set(ByVal value As Color)
            moFrameColor = value
        End Set
    End Property

    '---XAxis Label Area Color
    ''' <summary>
    ''' Sets or gets the background color of the area at the bottom of the 
    ''' chart reserved for X Axis labels.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to Color.White</remarks>
    Public Property XAxisBackgroundColor() As Color
        Get
            Return moXAxisBackgroundColor
        End Get
        Set(ByVal value As Color)
            moXAxisBackgroundColor = value
        End Set
    End Property

    '---YAxis 1 Label Area Color
    ''' <summary>
    ''' Sets or gets the background color of the area at the left side of the chart reserved for Y Axis 1 labels.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to Color.White</remarks>
    Public Property YAxis1BackgroundColor() As Color
        Get
            Return moYAxis1BackgroundColor
        End Get
        Set(ByVal value As Color)
            moYAxis1BackgroundColor = value
        End Set
    End Property

    '---YAxis 2 Label Area Color
    ''' <summary>
    ''' Sets or gets the background color of the area at the right side of the chart reserved for Y Axis 2 labels.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>Defaults to Color.White</remarks>
    Public Property YAxis2BackgroundColor() As Color
        Get
            Return moYAxis2BackgroundColor
        End Get
        Set(ByVal value As Color)
            moYAxis2BackgroundColor = value
        End Set
    End Property

    Friend Sub DrawBackground(ByRef g As Graphics)
        Dim oBrush As Brush
        Dim oRect As Rectangle

        oBrush = New SolidBrush(moFrameColor)
        oRect = New Rectangle(0, 0, miOuterWidth, miOuterHeight)
        g.FillRectangle(oBrush, oRect)

        oBrush = New SolidBrush(moBackgroundColor)
        oRect = New Rectangle(miInnerLeft, miInnerTop, miInnerWidth, miInnerHeight)
        g.FillRectangle(oBrush, oRect)
    End Sub

    Public Function Copy() As ChartDimensions
        Dim oChartDimensions As New ChartDimensions(miOuterWidth, miOuterHeight)
        oChartDimensions.Calibrate()

        With oChartDimensions
            .BackgroundColor = Me.BackgroundColor
            .FrameColor = Me.FrameColor
            .FrameWidthBottom = Me.FrameWidthBottom
            .FrameWidthRight = Me.FrameWidthRight
            .FrameWidthTop = Me.FrameWidthTop
            .InnerBottom = Me.InnerBottom
            .InnerHeight = Me.InnerHeight
            .InnerLeft = Me.InnerLeft
            .InnerRight = Me.InnerRight
            .InnerTop = Me.InnerTop
            .InnerWidth = Me.InnerWidth
            .OuterHeight = Me.OuterHeight
            .OuterWidth = Me.OuterWidth
            .OuterX = Me.OuterX
            .OuterY = Me.OuterY
            .TitleHeight = Me.TitleHeight
            .XAxisBackgroundColor = Me.XAxisBackgroundColor
            .XAxisLabelRowHeight = Me.XAxisLabelRowHeight
            .YAxis1BackgroundColor = Me.YAxis1BackgroundColor
            .YAxis1LabelColumnWidth = Me.YAxis1LabelColumnWidth
            .YAxis2BackgroundColor = Me.YAxis2BackgroundColor
            .YAxis2LabelColumnWidth = Me.YAxis2LabelColumnWidth
        End With

        Return oChartDimensions
    End Function
End Class
